home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / I8255F09.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  4KB  |  108 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   I8255F08.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.00.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *              :   Toggle Output Bit Function
  9.  *  ----------------------------------------------------------------------
  10.  */
  11.  
  12. #define  I8255F08_C_DEFINED  1
  13. #include "I8255FN.H"
  14. #undef   I8255F08_C_DEFINED
  15.  
  16. void I8255_bittog (I8255DAT *data, int bit);
  17.  
  18. /*- I8255 : Toggle Output Bit ----------------**
  19.  *  Toggle one of the output bits in the 8255.
  20.  *  The bit state is toggled.
  21.  *  The bit number should be from 1 - 24 as follows:
  22.  *   1 = Port A Bit 0;   8 = Port A Bit 7
  23.  *   9 = Port B Bit 0;  16 = Port B Bit 7
  24.  *  17 = Port C Bit 0;  24 = Port C Bit 7
  25.  *  Note:
  26.  *  0 XOR 0 = 0; 1 XOR 0 = 1 => Do not affect other bits.
  27.  *  0 XOR 1 = 1; 1 XOR 1 = 0 => Affect toggle bit.
  28.  *  Passed:
  29.  *      pointer :   I8255DAT
  30.  *      integer :   bit number
  31.  *  Returns:
  32.  *      nothing
  33.  *      Loads stat with appropriate error code
  34.  */
  35. void I8255_bittog (I8255DAT *data, int bit)
  36.     {
  37.     int             port;   /* port number  */
  38.     int             padd;   /* port address */
  39.     unsigned char   mask;   /* byte mask    */
  40.     unsigned char   oval;   /* out value    */
  41.  
  42.     /*  Make sure the bit requested is valid.
  43.      *  Three ports of 8 bits each = 24 bits.
  44.      */
  45.     if ( (bit < 1) || (bit > 24) ){
  46.         data->stat = I8255_ST_BB;
  47.         return;
  48.         }
  49.  
  50.     /*  Decrement the bit so we have zero offset.
  51.      *  This is useful for lining up the port with integer
  52.      *  divide and it is also used for left shifting to
  53.      *  create a mask.
  54.      *  Get port number by doing integer division.  The port
  55.      *  number should end up as 0, 1, or 2, corresponding to
  56.      *  PORT A, PORT B, and PORT C respectively.
  57.      *  Then do modulo 8 to get the corrsponding bit
  58.      *  number to be used in the particular byte.  This
  59.      *  value will end up being 0 - 7.
  60.      *  Then form the bit mask, starting with 1 and
  61.      *  left shifting to line it up with the proper bit.
  62.      *  There will then be a set bit in the mask byte corresponding
  63.      *  to the bit in the 8255 PORT to be toggled.  All other bits in
  64.      *  the mask byte will be zero.  The current data is XORED with
  65.      *  the mask and then output.
  66.      */
  67.     bit--;
  68.     port = ( bit / 8 );     /* port number (0 - 2)          */
  69.     bit = bit % 8;          /* modulo to get the bit number */
  70.     mask = 0x01;            /* assume low bit of byte       */
  71.     mask = mask << bit;     /* shift left to requested bit  */
  72.  
  73.     /*  Switch to appropriate port and
  74.      *  XOR port byte with mask byte.
  75.      *  Obtain port address.
  76.      */
  77.     switch ( port ){
  78.         case 0:         /* port A   */
  79.             data->adat = data->adat ^ mask;
  80.             oval = data->adat;
  81.             padd = I8255_PORTA (data->base);
  82.             break;
  83.         case 1:         /* port B   */
  84.             data->bdat = data->bdat ^ mask;
  85.             oval = data->bdat;
  86.             padd = I8255_PORTB (data->base);
  87.             break;
  88.         case 2:         /* port C   */
  89.             data->cdat = data->cdat ^ mask;
  90.             oval = data->cdat;
  91.             padd = I8255_PORTC (data->base);
  92.             break;
  93.         default:        /* bad port number  */
  94.             data->stat = I8255_ST_BP;
  95.             return;
  96.             break;
  97.         }
  98.  
  99.     chp_portwt ( padd, oval );
  100.     data->stat = I8255_ST_OK;
  101.     }
  102.  
  103. /*-
  104.  *  ----------------------------------------------------------------------
  105.  *  END I8255F09.C Source File
  106.  *  ----------------------------------------------------------------------
  107.  */
  108.